<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  install
 */

class m_install_actions_CompleteAction extends f_core_Action
{
	public function execute()
	{
        $this->addToModel('key', m_backoffice_services_BackOfficeService::getKey());
        $config = f_loader_Config::getInstance();
        $config->setParameter('default', 'default_module', 'website');
        $config->setParameter('default', 'default_action', 'Display');
        $config->save();
        $this->setView('success');
	}

	public function validate()
	{
        if(!f_database_Provider::checkConnect())
        {
            return false;
        }
        if(!m_users_services_UserService::getInstance()->hasAdminUser())
        {
            return false;
        }

        $documentProvider = f_document_Provider::getInstance();

        if($documentProvider->loadDocuments("modules_website/website")->count() == 0)
        {
            return false;
        }

        $config = f_loader_Config::getInstance();
        if($config->getParameter('default', 'default_module') == 'install' && $config->getParameter('default', 'default_action') == 'Install')
        {
            return true;
        }
        return false;
	}

	public function errorHandler()
	{
        if(!f_database_Provider::checkConnect())
        {
            $this->addToModel('error', 'database');
        }
        else if(!m_users_services_UserService::getInstance()->hasAdminUser())
        {
            $this->addToModel('error', 'user');
        }
        else
        {
            $documentProvider = f_document_Provider::getInstance();

            if($documentProvider->loadDocuments("modules_website/website")->count() == 0)
            {
                $this->addToModel('error', 'website');
            }

            $config = f_loader_Config::getInstance();
            if($config->getParameter('default', 'default_module') != 'install' && $config->getParameter('default', 'default_action') != 'Install')
            {
                $this->addToModel('error', 'success');
            }
        }


        $this->setView('error');
	}
}